const { query } = require('../config/db');

//  根据板块 id 查找全部帖子
async function section(sid) {
  const sql = `select sid,pid,name,intro,story,portrait from post where sid = '${sid}'`;
  return await query(sql);
}
//  根据 sid 和 pid 查找帖子
async function post(sid, pid) {
  const sql = `select sid,pid,name,intro,story,portrait from post where sid = '${sid}' and pid = '${pid}'`;
  return await query(sql);
}
//  根据 pid 查找帖子是否收藏
async function isCollect(uid, pid) {
  const sql = `select uid from collected where uid = '${uid}' and pid = '${pid}'`;
  return await query(sql);
}
//  根据内容查找帖子
async function selectPost(intro) {
  const sql = `select sid,pid,intro,story from post where intro like '%${intro}%' or name like '%${intro}%'`;
  return await query(sql);
}
//  根据用户 id 查找帖子
async function uPost(uid) {
  const sql = `select p.sid,p.pid,p.name,p.portrait from post p,(select pid from collected where uid = '${uid}') t where p.pid = t.pid`;
  return await query(sql);
}
//  根据 pid 收藏帖子
async function addPost(uid, pid) {
  const sql = `insert into collected (uid,pid) values ('${uid}','${pid}')`;
  return await query(sql);
}
// 删除板块数据
async function noneRecycle(uid, pid) {
  const sql = `delete from collected where uid = '${uid}' and pid = '${pid}';`;
  return await query(sql);
}

module.exports = {
  section, post, isCollect, selectPost, uPost, addPost, noneRecycle
}